package com.yeyi.ytest.java8.stream;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * 测试 stream.max
 * @author yeyi
 */
public class StreamMaxAndSort {
	static class Test{
		Integer i;
		String s;
		
		public Test(Integer i, String s) {
			super();
			this.i = i;
			this.s = s;
		}

		@Override
		public String toString() {
			return "Test [i=" + i + ", s=" + s + "]";
		}

		public Integer getI() {
			return i;
		}

		public void setI(Integer i) {
			this.i = i;
		}

		public String getS() {
			return s;
		}

		public void setS(String s) {
			this.s = s;
		}
		
	}
	public static void main(String[] args) {
		List<Test> tests = new ArrayList();
		tests.add(new Test(1,"a"));
		tests.add(new Test(8,"b"));
		tests.add(new Test(3,"f"));
		tests.add(new Test(4,"d"));
		tests.add(new Test(5,"e"));
		// 排序 reverseOrder： 倒序
		Object [] objs = tests.stream().sorted(Comparator.comparing(Test::getI, Comparator.reverseOrder()).thenComparing(Test::getS)).toArray();
		System.out.println(JSON.toJSONString(objs, true));

		List<Test> ls = tests.stream().sorted(Comparator.comparingInt(Test::getI).thenComparing(Test::getS)).collect(Collectors.toList());
		System.out.println(JSON.toJSONString(ls, true));

		Test maxI = tests.stream().max(new Comparator<Test>() {  
  
            @Override  
            public int compare(Test o1, Test o2) {  
                 return o1.i - o2.i; // 注意这里的顺序
            }  
  
        }).get();
		
		Test maxS = tests.stream().max(new Comparator<Test>() {  
  
            @Override  
            public int compare(Test o1, Test o2) {  
                 return o1.s.compareTo(o2.s);  
            }  
  
        }).get();
		
		System.out.println("maxI: "+maxI);
		System.out.println("maxS: "+maxS);
	}
}
